﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="recSanctionform.aspx.cs"
    Inherits="SIPO.Web.Forms.Transaction.recSanctionformCopy" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Sanksi Form</title>

    <link href="<%= ResolveUrl("~/styles/chosen/chosen.min.css") %>" rel="stylesheet" type="text/css" />

    <script src="<%= ResolveUrl("~/scripts/plupload/moxie.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/plupload/plupload.full.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>

    <script src="<%= ResolveUrl("~/scripts/chosen/chosen.jquery.min.js") %>" type="text/javascript"></script>

    <style type="text/css">
        .style1
        {
            width: 225px;
        }
        .style2
        {
            width: 111px;
        }
    </style>
    <script type="text/javascript" language="javascript">

        $(function () {

            $('#ddlDebtor').chosen({ placeholder_text_single: "-- pilih debitur --" });
            $('#ddlSubSector').chosen({ placeholder_text_single: "-- pilih sub sektor --" });
            $('#ddlDebtorSector').chosen({ placeholder_text_single: "-- pilih sub sub sektor --" });
            $('#ddlSanctionType').chosen({ placeholder_text_single: "-- pilih tipe sangsi --" });

        });

    </script>
    <script type="text/javascript">

        $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });
        $("#btnClose").button({ icons: { primary: "ui-icon-close"} });

        $('#btnClose').click(function () {
            parent.$('#dialog').dialog("close");

            return false;
        });
 
    </script>
    <script type="text/javascript">

        $(document).ready(function () {

            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight',
                browse_button: 'pickfiles',
                container: 'container',
                max_file_size: '5mb',
                url: '<%= ResolveUrl("~/AjaxHandler/Transaction/recSanctionHandler.ashx") %>' + '?method=insert',
                flash_swf_url: '<%= ResolveUrl("~/scripts/plupload/Moxie.swf") %>',
                silverlight_xap_url: '<%= ResolveUrl("~/scripts/plupload/Moxie.xap") %>',
                filters: [
            { title: "Pdf files", extensions: "pdf" },
            { title: "Excel files", extensions: "xls,xlsx" },
			{ title: "Text files", extensions: "txt" },
			{ title: "Image files", extensions: "jpeg,jpg,gif,png" },
            { title: "Word files", extensions: "doc,docx" },
			{ title: "PowerPoint files", extensions: "ppt,pptx" }
		],
                resize: { width: 320, height: 240, quality: 90 }
            });

            uploader.bind('Init', function (up, params) {

                var msg = 'Untuk dapat melakukan proses unggah / upload dokumen, browser anda harus mendukung salah satu teknologi, HTML 5 / Flash / Silverlight';

                if (params.runtime == 'html5') {
                    msg = '';
                } else if (params.runtime == 'flash') {
                    msg = '';
                } else if (params.runtime == 'silverlight') {
                    msg = ''
                }

                if (msg != '') {
                    alert(msg);
                }

            });

            uploader.init();

            uploader.bind('FilesAdded', function (up, files) {

                if (up.files.length > 1) {
                    alert('Hanya diperbolehkan satu file untuk di upload!');
                    return false;
                }

                $.each(files, function (i, file) {
                    $('#filelist').append(
                        '<div id="' + file.id + '">' +
                file.name + ' (' + plupload.formatSize(file.size) + ') <b></b>' + '</div>');
                });

                up.refresh(); // Reposition Flash/Silverlight
            });

            uploader.bind('FileUploaded', function (up, file, info) {

                var err = JSON.parse(info.response);
                alert(err.Message);

            });

            uploader.bind('Error', function (up, err) {

                var errMsg = '';

                switch (err.code) {
                    case (-600):
                        errMsg = 'File tidak boleh lebih dari 5 Mb';
                        break;
                    case (-601):
                        errMsg = 'File harus berformat excel / word/ text / pdf / gambar';
                        break;
                    default:
                        errMsg = err.code + ' - ' + err.message;
                        break;
                }

                alert(errMsg);

                up.refresh(); // Reposition Flash/Silverlight

            });

            $("#txtLetterNo").inputmask("Regex", { regex: "[A-Za-z-_.0-9\/]*" });
            $('#txtLetterNo').upperCase();

            $("#txtValue").inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });

            var date = new Date();
            var currentMonth = date.getMonth();
            var currentDate = date.getDate();
            var currentYear = date.getFullYear();

            $("#txtLetterDate").datepicker({ showWeek: true, showButtonPanel: true,
                changeMonth: true, changeYear: true, dateFormat: 'dd/mm/yy', // yearRange: '1990:2030'
                maxDate: new Date(currentYear, currentMonth, currentDate)
            });

            $("#txtLetterDate").inputmask("d/m/y", { "placeholder": "dd/mm/yyyy" });

            var id = $('#hfID').val();

            // 1. prepare the validation rules and messages.
            var rules = {
                ddlDebtorSector: "required",
                ddlSanctionType: "required",
                ddlDebtor: "required",
                txtValue: "required",
                txtLetterNo: "required",
                txtLetterDate: "required"
            };

            var messages = {
                ddlDebtorSector: "Sektor Debitur harus di isi",
                ddlSanctionType: "Tipe sanksi harus di isi",
                ddlDebtor: "Debitur harus di isi",
                txtValue: "Jumlah harus di isi",
                txtLetterNo: "No surat harus di isi",
                txtLetterDate: "Tgl. Surat harus di isi"
            };

            // 2. Initiate the validator
            var validator = new jQueryValidatorWrapper("form1", rules, messages);

            // 3. Set the click event to do the validation
            $("#btnSave").click(function (e) {

                e.preventDefault();

                if (!validator.validate())
                    return;

                var invoiceno = $('#hfInvoiceNo').val();

                if (invoiceno == '') {

                    var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recSanctionHandler.ashx") %>';

                    if (id == '') {

                        if (uploader.files.length > 0) // Insert with uploaded file.
                        {
                            uploader.settings.multipart_params = {
                                'ddlDebtorSector': encodeURIComponent($('#ddlDebtorSector').val()),
                                'ddlSanctionType': encodeURIComponent($('#ddlSanctionType').val()),
                                'txtLetterNo': encodeURIComponent($('#txtLetterNo').val()),
                                'txtLetterDate': encodeURIComponent($('#txtLetterDate').val()),
                                'hfID': encodeURIComponent($('#hfID').val()),
                                'txtValue': encodeURIComponent($('#txtValue').val())
                            };

                            uploader.settings.url = handlerUrl + '?method=insert';
                            uploader.start();
                        }
                        else {

                            $.post('<%= ResolveUrl("~/AjaxHandler/Transaction/recSanctionHandler.ashx") %>' + '?method=insert',
                                $("#form1")
                                .find("input,textarea,select,hidden")
                                .not("#__VIEWSTATE,#__EVENTVALIDATION")
                                .serialize())
                            .done(function (data) {
                                var r = JSON.parse(data);
                                alert(r.Message);
                            })
                            .fail(function () {
                                alert('Data gagal di simpan!');
                            });

                        }

                    }
                    else {

                        if (uploader.files.length > 0) // Update with uploaded file.
                        {
                            uploader.settings.multipart_params = {
                                'ddlDebtorSector': encodeURIComponent($('#ddlDebtorSector').val()),
                                'ddlSanctionType': encodeURIComponent($('#ddlSanctionType').val()),
                                'txtLetterNo': encodeURIComponent($('#txtLetterNo').val()),
                                'txtLetterDate': encodeURIComponent($('#txtLetterDate').val()),
                                'hfID': encodeURIComponent($('#hfID').val()),
                                'txtValue': encodeURIComponent($('#txtValue').val())
                            };

                            uploader.settings.url = handlerUrl + '?method=update';
                            uploader.start();

                        }
                        else {

                            $.post('<%= ResolveUrl("~/AjaxHandler/Transaction/recSanctionHandler.ashx") %>' + '?method=update',
                                $("#form1")
                                .find("input,textarea,select,hidden")
                                .not("#__VIEWSTATE,#__EVENTVALIDATION")
                                .serialize())
                            .done(function (data) {
                                var r = JSON.parse(data);
                                alert(r.Message);
                            })
                            .fail(function () {
                                alert('Data gagal di ubah!');
                            });

                        }

                    }

                    parent.$('#dialog').dialog('close');

                    setTimeout("parent.$('#table').trigger('reloadGrid');", 3000);

                    return false;

                }
                else {
                    alert("Data tidak bisa dirubah dikarenakan No. Faktur sudah terdafdar !");
                }
            });

        });
    
    </script>
    <script type="text/javascript">

        $('#ddlDebtor').change(function () {
            getSubSector();

        });

        $('#ddlSubSector').change(function () {
            getSubSubSector();
        });

        function getSubSector() {

            var debtorid = $("#ddlDebtor option:selected").val();
            $subSectors = $('#ddlSubSector');
            $debtorsector = $('#ddlDebtorSector');

            $subSectors.get(0).options.length = 0;
            $subSectors.get(0).options[0] = new Option("-- pilih sub sektor --", "");
            $subSectors.trigger('chosen:updated');

            $debtorsector.get(0).options.length = 0;
            $debtorsector.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
            $debtorsector.trigger('chosen:updated');

            if (debtorid > 0) {

                // ambil data sub sector dari tabel debtor sector where debtor id
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getdebtorsector.ashx?method=getsubsectorbydebtor") %>';
                $.ajax({
                    type: 'GET',
                    url: handlerUrl + "&debtor_id=" + debtorid,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (datas) {

                        if (datas.length > 1) {

                            $.each(datas, function (i, data) {
                                mappingSubSector(data.SubSectorID);
                            });

                        }
                        else {

                            var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsector.ashx?method=getbyid&subsector_id=") %>';
                            $.ajax({
                                type: 'GET',
                                url: handlerUrl + datas[0].SubSectorID,
                                contentType: 'application/json',
                                dataType: 'json',
                                success: function (datasubs) {

                                    $.each(datasubs, function (i, datasub) {
                                        $subSectors.append('<option value="' + datasub.SubSectorID + '">' + datasub.SubSectorCode + ' - ' + datasub.SubSectorName + '</option>');
                                    });

                                    $subSectors.val(datas[0].SubSectorID);
                                    $subSectors.trigger('chosen:updated');

                                    getSubSubSector();

                                },
                                error: function () {
                                    alert('Error load sub sector !');
                                }
                            });

                        }
                    },
                    error: function () {
                        alert('Error load sub sector from debtor sector!');
                    }
                });
            }

        }

        function mappingSubSector(SubSectorID) {

            var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsector.ashx?method=getbyid&subsector_id=") %>';
            $subSectors = $('#ddlSubSector');
            $.ajax({
                type: 'GET',
                url: handlerUrl + SubSectorID,
                contentType: 'application/json',
                dataType: 'json',
                success: function (datasubs) {

                    $.each(datasubs, function (i, datasub) {
                        $subSectors.append('<option value="' + datasub.SubSectorID + '">' + datasub.SubSectorCode + ' - ' + datasub.SubSectorName + '</option>');
                    });

                    $subSectors.trigger("chosen:updated");

                },
                error: function () {
                    alert('Error load sub sector !');
                }
            });

        }

        function getSubSubSector() {

            var debtorId = $("#ddlDebtor option:selected").val();
            var subSectorId = $("#ddlSubSector option:selected").val();

            $debtorsector = $('#ddlDebtorSector');

            $debtorsector.get(0).options.length = 0;
            $debtorsector.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
            $debtorsector.trigger('chosen:updated');

            if (debtorId > 0 && subSectorId > 0) {

                //ambil data sub sub sector dari debtor sector berdasar debtor id dan sub sector
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getdebtorsector.ashx?method=getbydebtor&debtor_id=") %>';

                $.ajax({
                    type: 'GET',
                    url: handlerUrl + debtorId + "&subsector_id=" + subSectorId,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (datas) {
                        if (datas.length > 1) {

                            $.each(datas, function (i, data) {
                                mappingSubSubSector(data.DebtorSectorID, data.SubSubSectorID, subSectorId);
                            });                            

                        }

                        else {

                            
                            var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsubsector.ashx?method=getbyidandsubsectorid&subsubsector_id=") %>';
                            $.ajax({
                                type: 'GET',
                                url: handlerUrl + datas[0].SubSubSectorID + "&subsector_id=" + subSectorId,
                                contentType: 'application/json',
                                dataType: 'json',
                                success: function (datasubsubs) {

                                    $.each(datasubsubs, function (i, datasubsub) {
                                        $debtorsector.append('<option value="' + datas[0].DebtorSectorID + '">' + datasubsub.SubSubSectorID + ' - ' + datasubsub.SubSubSectorName + '</option>');
                                    });

                                    $debtorsector.val(datas[0].DebtorSectorID);
                                    $debtorsector.trigger('chosen:updated');

                                },
                                error: function () {
                                    alert('Error Mapping data sub sub sektor!');
                                }
                            });
                        }


                    },
                    error: function () {
                        alert('Error getSubSubSector !');
                    }
                });
            }
            else {
                $debtorsector.get(0).options.length = 0;
                $debtorsector.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
                $debtorsector.trigger('chosen:updated');
            }

        }

        function mappingSubSubSector(DebtorSectorID, SubSubSectorID, subsectorid) {

            /// maping data sub sub sector dari debtor sector ke tabel subsubsector buat deskripsi combo,value combo ini di isi dengan debtorsectorid
            $debtorsector = $('#ddlDebtorSector');

            if (SubSubSectorID > 0) {
                var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Loaddata/getsubsubsector.ashx?method=getbyidandsubsectorid&subsubsector_id=") %>';
                $.ajax({
                    type: 'GET',
                    url: handlerUrl + SubSubSectorID + "&subsector_id=" + subsectorid,
                    contentType: 'application/json',
                    dataType: 'json',
                    success: function (datasubsubs) {

                        $.each(datasubsubs, function (i, datasubsub) {
                            $debtorsector.append('<option value="' + DebtorSectorID + '">' + datasubsub.SubSubSectorID + ' - ' + datasubsub.SubSubSectorName + '</option>');
                        });

                        $debtorsector.trigger("chosen:updated");

                    },
                    error: function () {
                        alert('Error mappingSubSubSector!');
                    }
                });
            }
            else {
                $debtorsector.get(0).options.length = 0;
                $debtorsector.get(0).options[0] = new Option("-- pilih sub sub sektor --", "");
                $debtorsector.trigger("chosen:updated");
            }

        }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td style="width: 50">
                Debitur
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlDebtor" runat="server" Width="300">
                </asp:DropDownList>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td>
                Sub Sektor
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlSubSector" runat="server" Width="300">
                </asp:DropDownList>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td>
                Sub Sub Sektor
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlDebtorSector" runat="server" Width="300">
                </asp:DropDownList>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td>
                Tipe Sanksi
            </td>
            <td>
                :
            </td>
            <td>
                <asp:DropDownList ID="ddlSanctionType" runat="server" Width="300">
                </asp:DropDownList>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td>
                Jumlah
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtValue" Style="text-align: right" runat="server" Width="200"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td>
                No. Surat Sanksi
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtLetterNo" runat="server" MaxLength="20" Width="100"></asp:TextBox>&nbsp;<span
                    style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td>
                Tgl Surat Sanksi
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtLetterDate" runat="server" Width="100"></asp:TextBox>
                &nbsp;<span style="color: Red;">*</span>
            </td>
        </tr>
        <tr>
            <td>
                Lampiran
            </td>
            <td>
                :
            </td>
            <td>
                <div id="container">
                    <a id="pickfiles" class="plupload_button plupload_add ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary"
                        href="#"><span class="ui-button-icon-primary ui-icon ui-icon-circle-plus" /><span
                            class="ui-button-text">Add Files</span> </a>
                    <br />
                    <div id="filelist"></div>
                </div>
            </td>
        </tr>
        <tr runat="server" id="trInvoice" visible="false">
            <td>
                No. Surat Setoran
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtInvoiceNo" Enabled="false" runat="server" Width="300"></asp:TextBox>
            </td>
        </tr>
        <tr runat="server" id="trInvoiceDate" visible="false">
            <td>
                Tgl. Surat Setoran
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtInvoiceDate" Enabled="false" runat="server" Width="100"></asp:TextBox>
            </td>
        </tr>
        <tr runat="server" id="trPayment" visible="false">
            <td>
                No. Pembayaran
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtPaymentNo" Enabled="false" runat="server" Width="300"></asp:TextBox>
            </td>
        </tr>
        <tr runat="server" id="trPaymentDate" visible="false">
            <td>
                Tgl Pembayaran
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtPaymentDate" Enabled="false" runat="server" Width="100"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <div style="text-align: right; padding-top: 10px;">
                    <button id="btnSave" type="submit">
                        Save</button>
                    <button id="btnClose" type="button">
                        Close</button>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
            <td>
                <asp:HiddenField ID="hfID" runat="server" />
                <asp:HiddenField ID="hfInvoiceNo" runat="server" />
            </td>
        </tr>
    </table>
    </form>
</body>
</html>
